#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project：kuberCloudTest 
@File：privatenetwork.py
@Author：admin
@Date：2024/4/11 17:00 
"""
from typing import List

from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getOperationType
from kuberCloudTest.networks.urls import networkUrls


class privateNetworkUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return privateNetworkUtils(accessToken=accessToken)


def getPrivateNetwork(accessToken, **kwargs):
    """
    获取私有网络
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 0: 私有网络名称; 1: 私有网络uuid
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getPrivateNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'searchitem': kwargs.get('searchItem', 0),
                                                         })


def getL2NetworkByPrivateNetwork(accessToken, **kwargs):
    """
    获取私有网络下的L2网络
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getL2NetworkByPrivateNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'searchitem': 0,
                                                         })


def createPrivateNetwork(accessToken, **kwargs):
    """
    创建私有网络
    :param accessToken:
    :param kwargs: name: xxx
                   description: xxx
                   l2NetworkUuid: xxx
    :return:
    """
    pass


def updatePrivateNetwork(accessToken, privateNetworkId, name, remark):
    """
    更新私有网络
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param name: 私有网络名称
    :param remark: 描述
    :return:
    """
    operationType = 5402
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['pnname'] = name
    data['remark'] = remark
    data['nid'] = privateNetworkId

    return get_utils(accessToken).cloud_http_request.post(networkUrls.updatePrivateNetwork.value, data=data)


def deletePrivateNetwork(accessToken, privateNetworkIds: List[str]):
    """
    删除私有网络
    :param accessToken:
    :param privateNetworkIds: 私有网络UUID(数组)
    :return:
    """
    operationType = 5403
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['ids'] = privateNetworkIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deletePrivateNetwork.value, data=data)


def globalSharePrivateNetwork(accessToken, privateNetworkIds):
    """
    全局共享私有网络
    :param accessToken:
    :param privateNetworkIds: 私有网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5413
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = privateNetworkIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalSharePrivateNetwork.value, data=data)


def globalRecallPrivateNetwork(accessToken, privateNetworkIds):
    """
    全局撤回私有网络
    :param accessToken:
    :param privateNetworkIds: 私有网络UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5414
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = privateNetworkIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalRecallPrivateNetwork.value, data=data)


def updatePrivateNetworkMTU(accessToken, privateNetworkId, mtu):
    """
    更新私有网络MTU
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param mtu: MTU
    :return:
    """
    operationType = 5412
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = privateNetworkId
    data['mtu'] = mtu

    return get_utils(accessToken).cloud_http_request.post(networkUrls.updatePrivateNetworkMTU.value, data=data)


def loadVMByPrivateNetwork(accessToken, privateNetworkId, vmIds: List[str]):
    """
    私有网络挂载虚拟机
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param vmIds: 云主机UUID（数组格式）
    :return:
    """
    operationType = 5406
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = privateNetworkId
    data['ids'] = vmIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.loadVMByPrivateNetwork.value, data=data)


def unloadVMByPrivateNetwork(accessToken, privateNetworkId, vmIds: List[str]):
    """
    私有网络卸载虚拟机
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param vmIds: 云主机UUID（数组格式）
    :return:
    """
    operationType = 5407
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = privateNetworkId
    data['ids'] = vmIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.unloadVMByPrivateNetwork.value, data=data)


def addPrivateNetworkRange(accessToken, privateNetworkId, **kwargs):
    """
    添加私有网络IP段
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param kwargs: startIp: xxx
                   endIp: xxx
                   gateway: xxx
                   netmask: xxx
                   dns: xxx
                   domain: xxx
                   ipRangeUuid: xxx
    :return:
    """
    operationType = 5404
    data = _getOperationType(operationType, AsyncType.ASYNC.value)


def deletePrivateNetworkRange(accessToken, privateNetworkId, segmentIds: list):
    """
    删除私有网络IP段
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param segmentIds: 三层网络段uuid(多个使用英文逗号分割)
    :return:
    """
    operationType = 5405
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = privateNetworkId
    data['ids'] = segmentIds

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deletePrivateNetworkRange.value, data=data)


def addPrivateNetworkDns(accessToken, privateNetworkId, dns, ipType):
    """
    添加私有网络DNS
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param dns: DNS
    :param ipType: 网络地址类型
                        1: IPV4
                        2: IPV6
    :return:
    """
    operationType = 5408
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = privateNetworkId
    data['dns'] = dns
    data['iptype'] = ipType

    return get_utils(accessToken).cloud_http_request.post(networkUrls.addPrivateNetworkDns.value, data=data)


def deletePrivateNetworkDns(accessToken, privateNetworkId, dns):
    """
    删除私有网络DNS
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param dns: DNS(多个使用英文逗号分割)
    :return:
    """
    operationType = 5409
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['nid'] = privateNetworkId
    data['dnss'] = dns

    return get_utils(accessToken).cloud_http_request.post(networkUrls.deletePrivateNetworkDns.value, data=data)


def getVMByPrivateNetwork(accessToken, privateNetworkId, **kwargs):
    """
    获取私有网络下的虚拟机
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVMByPrivateNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': privateNetworkId
                                                         })


def getVMNotinPrivateNetwork(accessToken, privateNetworkId, **kwargs):
    """
    获取私有网络未挂载的虚拟机
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param kwargs: limit:
                   offset:
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVMNotinPrivateNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': privateNetworkId
                                                         })


def getNetworkRangeByPrivateNetwork(accessToken, privateNetworkId, **kwargs):
    """
    获取私有网络下的IP段
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 1:IPV4;  2:IPV6; 3:双栈；
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getNetworkRangeByPrivateNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'nid': privateNetworkId,
                                                             'type': kwargs.get('keyword', 1)
                                                         })


def getDNSByPrivateNetwork(accessToken, privateNetworkId, **kwargs):
    """
    通过私有网络id查询DNS
    :param accessToken:
    :param privateNetworkId: 私有网络UUID
    :param kwargs: limit:
                   offset:
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getDNSByPrivateNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': privateNetworkId
                                                         })
